<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :obj="compantInfo" :is-root="true" @add-dept="addDept" />

        <!-- //^树形结构 -->
        <!-- default-expand-all是否打开树形结构 -->
        <el-tree :data="treeData" :props="defaultProps" :default-expand-all="true">
          <!-- 说明el-tree里面的这个内容 就是插槽内容 => 填坑内容  => 有多少个节点循环多少次 -->
          <!-- scope-scope 是 tree组件传给每个节点的插槽的内容的数据 -->
          <!-- 顺序一定是 执行slot-scope的赋值 才去执行 props的传值 -->
          <tree-tools slot-scope="{ data }" :obj="data" @add-dept="addDept" @edit-dept="editDept" />
        </el-tree>
      </el-card>
    </div>

    <!-- 添加弹出框 -->
    <add-dept ref="addDepts" v-model="addDialogVisible" :tree-data="Data" @success-add="getDepartmentsList" @success-edit="getDepartmentsList" />

  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments.js'
import { tranListToTree } from '@/utils'
import AddDept from './components/add-dept.vue'

export default {
  components: { treeTools, AddDept },
  data() {
    return {
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      compantInfo: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      // 弹出框
      addDialogVisible: false,
      Data: {}
    }
  },

  created() {
    this.getDepartmentsList()
  },

  methods: {
    // 获取组织建构的数据
    async getDepartmentsList() {
      const res = await getDepartments()
      // console.log(res)
      this.treeData = tranListToTree(res.depts, '')
    },
    // 点击添加子部门
    addDept(data) {
      this.addDialogVisible = true
      // console.log(data)
      this.Data = data
    },
    editDept(data) {
      // 显示弹出层
      this.addDialogVisible = true
      // 传给子组件只是为了进行校验的
      this.Data = data
      // 获取最新的数据进行渲染到表单的
      this.$refs.addDepts.getNewData(data.id)
    }
  }
}
</script>

<style scoped lang="less" >
// .tree-card {
//   padding: 30px 140px;
//   font-size: 14px;
// }
</style>
